package com.example.aircraft.panel;

import androidx.appcompat.app.AlertDialog;


import android.app.Activity;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

import com.example.aircraft.MainActivity;
import com.example.aircraft.MyApplication;
import com.example.aircraft.R;
import com.example.aircraft.database.MySqlManager;
import com.example.aircraft.database.MySqliteHelper;
import com.example.aircraft.database.SQLiteInterface;

import java.io.IOException;

public class EnrollPanel extends Activity {
    private String userName;
    private String passWord;
    private int credits = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_enroll_panel);
    }

    public void Enroll(View view) {
//        boolean enrollsuccess = true;
        MyApplication.startSocketListener();
        //一系列检查操作
        EditText accountText= findViewById(R.id.editTextAccount1);
        this.userName = accountText.getText().toString();
        //获取输入的密码
        EditText passwordText= findViewById(R.id.editTextPassword1);
        this.passWord = passwordText.getText().toString();
        new Thread(){
            @Override
            public void run(){
                Log.i("client", "send message to server");
                MyApplication.getWriter().println("333_987#"+userName+","+passWord);
                MyApplication.getWriter().flush();
                String content = "";
                while(true){
                    try {
                        if (!((content=MyApplication.getIn().readLine())!=null)) break;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if("111#0,0".equals(content)){
                        Log.d("REGTEST","注册成功");
                        MySqlManager.updateData(userName);
                            Intent intent = new Intent(EnrollPanel.this, DualSignPanel.class);
                            startActivity(intent);
                            EnrollPanel.this.finish();
                            break;
                    }else if("404#1,1".equals(content)){
                        Log.d("REGTEST","注册失败");
                    }
                }
            }
        }.start();
        }

    }
